import {BasePage, BasePageData} from "../common/core/BasePage";
import {UserPage} from "../common/partPages/UserPage";
import {page, pageFunc} from "../common/PageBuilder";
import {User} from "../../modules/userModule/data/User";
import {DataLoader, field} from "../../modules/coreModule/data/DataLoader";
import {TaskIndexData} from "../../modules/taskModule/data/Task";
import {UserInterface} from "../../modules/userModule/UserCommon";
import {PK} from "../../modules/coreModule/data/BaseData";
import {alertMgr} from "../../modules/coreModule/managers/AlertManager";
import {waitForLogin} from "../../modules/userModule/managers/UserManager";
import {waitForDataLoad} from "../../modules/coreModule/managers/DataManager";
import {QueryPage} from "../common/partPages/QueryPage";
import {blockLoading, showLoading} from "../../modules/coreModule/managers/LoadingManager";
import {CoreLoading} from "../../modules/coreModule/CoreCommon";


class Data extends BasePageData {

    @field(User)
    user: User;

    @field([TaskIndexData])
    militaryTasks: TaskIndexData[];

    @field([TaskIndexData])
    partTimeTasks: TaskIndexData[];

    @field([TaskIndexData])
    rewardTasks: TaskIndexData[];

    @field(Number)
    taskCount: number

    @field(Number)
    followeeCount: number

    @field(Number)
    followerCount: number

    @field(Boolean)
    isFollowed: boolean;

    @field(Number)
    tabIdx = 0;
}

class Param {
    id: PK
}

@page("userDetail", "个人详情")
export class UserDetailPage extends BasePage<Data, Param> {

    public data = new Data();
    /**
     * 部分页
     */
    public userPage: UserPage = new UserPage();

    onLoad(e) {
        super.onLoad(e);
        this.loadData().then();
    }

    @showLoading(CoreLoading.Mask)
    @blockLoading
    @waitForLogin
    @waitForDataLoad
    private async loadData() {
        await this.loadUser();
    }

    private async loadUser() {
        let res = await UserInterface.GetDetail({id: this.params.id});
        await this.setData({
            user: DataLoader.load(User, res.user),
            militaryTasks: res.militaryTasks.map(t =>
                DataLoader.load(TaskIndexData, t)),
            partTimeTasks: res.partTimeTasks.map(t =>
                DataLoader.load(TaskIndexData, t)),
            rewardTasks: res.rewardTasks.map(t =>
                DataLoader.load(TaskIndexData, t)),
            taskCount: res.taskCount,
            followerCount: res.followerCount,
            followeeCount: res.followeeCount,
            isFollowed: res.isFollowed
        });
    }

    @pageFunc
    public follow() {
        UserInterface.FollowUser({openid: this.data.user._id, follow: !this.data.isFollowed}).then();
        this.setData({isFollowed: !this.data.isFollowed}).then();
        if (this.data.isFollowed) {
            alertMgr().showAlert("关注成功").then();
        } else {
            alertMgr().showAlert("取消关注成功").then();
        }
    }

    @pageFunc
    public onTabChange(e) {
        this.setData({tabIdx: e.detail.index}).then();
    }
}

